package com.gdth.bank.widget;

import java.io.File;

import com.gdth.bank.R;
import com.gdth.bank.activity.TestPicActivity;
import com.gdth.bank.utils.AppUtil;
import com.gdth.utils.BaseAppUtil;
import com.gdth.utils.PermissionUtil;
import com.gdth.utils.ToolUtil;
import com.gdth.utils.UUIDUtil;

import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

/**
 * 底部弹窗
 * @author mac1
 *
 */
public class PopupWindows extends PopupWindow{
	public static final String EXTRA_IMAGE_CLASS = "className";
	
	private final Activity mContext;
	
	public String savePath;
	public String uuid;
	public String wjlx;
	
	public int type;
	
	public PopupWindows(final Context mContext, View parent, int type){
		this(mContext, parent);
		
		this.type = type;
	}
	
	public PopupWindows(final Context mContext, View parent){
		super(mContext);
		
		this.mContext = (Activity) mContext;
		
		View view = View.inflate(mContext, R.layout.item_popupwindows, null);
		view.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_ins));
		LinearLayout ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
		ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_bottom_in_2));

		setWidth(LayoutParams.MATCH_PARENT);
		setHeight(LayoutParams.MATCH_PARENT);
//		setBackgroundDrawable(new BitmapDrawable());
		setFocusable(true);
		setOutsideTouchable(true);
		setContentView(view);
		showAtLocation(parent, Gravity.BOTTOM, 0, 0);
		update();

		Button btn_camera = (Button) view.findViewById(R.id.item_popupwindows_camera);
		Button btn_photo = (Button) view.findViewById(R.id.item_popupwindows_Photo);
		Button btn_cancel = (Button) view.findViewById(R.id.item_popupwindows_cancel);
		btn_camera.setOnClickListener(photoOnClickListener);
		btn_photo.setOnClickListener(pirtureOnClickListener);
		btn_cancel.setOnClickListener(cancleOnClickListener);
		
	    PermissionUtil.checkPermissionCamera(this.mContext);
	}
	
	/**
	 * 拍照
	 */
	OnClickListener photoOnClickListener = new OnClickListener(){
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			photo();
			dismiss();
		}
	};
	
	/**
	 * 从相册选择图片
	 */
	OnClickListener pirtureOnClickListener = new OnClickListener(){
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(mContext, TestPicActivity.class);
			String className = mContext.getClass().getName();
			
			if (type == 1){
				mContext.startActivityForResult(intent, AppUtil.activity_picture_requestcode);
			} else {
				intent.putExtra(EXTRA_IMAGE_CLASS, className);
				mContext.startActivity(intent);
			}
			dismiss();
		}
	};
	
	/**
	 * 取消
	 */
	OnClickListener cancleOnClickListener = new OnClickListener(){
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dismiss();
		}
	};

	/**
	 * 拍照
	 */
	public void photo(){
		String status=Environment.getExternalStorageState(); 
		if(status.equals(Environment.MEDIA_MOUNTED)) {
			Intent mIntent = new Intent();
			mIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
			mIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
			mIntent.putExtra(MediaStore.EXTRA_OUTPUT, getUri());//将拍取的照片保存到指定URI
			mContext.startActivityForResult(mIntent, AppUtil.activity_photo_requestcode);
//			Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//			mIntent.putExtra(MediaStore.EXTRA_OUTPUT, getUri());
//			mIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
//			mContext.startActivityForResult(mIntent, AppUtil.activity_photo_requestcode);
		}  else { 
			Toast.makeText(mContext, "没有储存卡",Toast.LENGTH_LONG).show(); 
		} 
	}
	
	private Uri getUri(){
		uuid = UUIDUtil.getUUID();
		wjlx = "jpg";
		String name = uuid + "." + wjlx;
		String path = ToolUtil.getFilePath(name);
		savePath = ToolUtil.getFileFullPath(path);
		File file = new File(savePath);
		Uri imageUri;
		if (Build.VERSION.SDK_INT >= 24) {
			imageUri =  FileProvider.getUriForFile(mContext, "com.jph.takephoto.fileprovider", file);//通过FileProvider创建一个content类型的Uri
		} else {
			imageUri = Uri.fromFile(file);
		}
		return imageUri;
	} 

}
